<template>
	<view class="page">
		<view class="list-wrapper">
			<!-- 列表项 -->
			<view class="list-item" v-for="(item, index) in listData" :key="index">
				<!-- 左侧图片 -->
				<image class="item-image" :src="baseImageUrl + item.publishImage.split(',')[0]" mode="aspectFill">
				</image>

				<!-- 右侧内容 -->
				<view class="item-content">
					<!-- 两行文字 -->
					<view class="item-title">{{item.publishDescription}}</view>
					<view class="item-desc">{{getFormatTime2(item.publishDate)}}</view>

					<!-- 操作按钮 -->
					<view class="item-actions">
						<text class="action-btn edit" @click="handleEdit(item)">编辑</text>
						<text class="action-btn delete" @click="handleDelete(item)">删除</text>
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		store,
		mutations
	} from '@/uni_modules/uni-id-pages/common/store.js'
	import configs from '../../../utils/config'
	import util from '@/utils/util'

	export default {
		computed: {
			userInfo() {
				return store.userInfo
			}
		},
		data() {
			return {
				listData: [],
				baseImageUrl: ""
			}
		},
		onLoad() {
			this.baseImageUrl = configs.IMAGE_URL;
			this.getAllPublish();

		},
		methods: {
			getFormatTime2(timestamp) {
				return util.getFormatTime(timestamp);
			},
			handleEdit(item) {
				uni.navigateTo({
					"url": '/pages/tabBar/publish/publish?type=edit&publishId='+item.publishId
				})
			},
			handleDelete(item) {
				let that = this;
				uni.showModal({
					title:'提示',
					content:'确认删除',
					success: function(res) {
						that.deletePublish(item.publishId)
					},
				})
			},
			getTime(time) {
				return;
			},
			deletePublish(id) {
				let that = this
				uniCloud.callFunction({
					name: 'page-main',
					data: {
						dataType: "deleteById",
						publishId:id
					},
					success(res) {
						if(res.result.code == 0) {
							that.getAllPublish()
						}
					},
					fail() {
				
					}
				})
			},
			getAllPublish() {
				let that = this
				uniCloud.callFunction({
					name: 'page-main',
					data: {
						dataType: "queryByUserName",
						userName: that.userInfo.nickname
					},
					success(res) {
						that.listData = res.result.list
					},
					fail() {

					}
				})
			},
		}
	}
</script>

<style>
	.page {
		padding: 20rpx;
	}

	.list-item {
		display: flex;
		padding: 20rpx;
		margin-bottom: 20rpx;
		background-color: #fff;
		border-radius: 10rpx;
	}

	.item-image {
		width: 200rpx;
		height: 200rpx;
		border-radius: 8rpx;
	}

	.item-content {
		flex: 1;
		margin-left: 20rpx;
		position: relative;
	}

	.item-title {
		font-size: 32rpx;
		font-weight: bold;
		margin-bottom: 10rpx;
	}

	.item-desc {
		font-size: 28rpx;
		color: #666;
	}

	.item-actions {
		position: absolute;
		bottom: 0;
		right: 0;
	}

	.action-btn {
		display: inline-block;
		padding: 10rpx 20rpx;
		margin-left: 20rpx;
		font-size: 24rpx;
		border-radius: 4rpx;
	}

	.edit {
		color: #007AFF;
	}

	.delete {
		color: #FF3B30;
	}
</style>